<?php
use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();
//单位
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();
//比率
$integral_rate = \app\models\Mall::getCurrentInstitutionIntegralRate();
//是否可以混合支付
$is_cash_order = \app\models\Mall::getCurrentInstitutionIsCashOrder();
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$institution_code = Mall::getCurrentInstitutionCode();
$this->title = $site_name . '-订单确认';
?>
    <script src="<?= $web ?>/js/IDValidator.js"></script>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/order-form.css"/>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/pay-window.css"/>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/await-pay.css"/>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/cross-title.css"/>
    <style type="text/css">
        .reminder {
            overflow: hidden;
            margin-left: 0.5rem;
            margin-right: 0.5rem;
        }

        .reminder .reminder-name {
            margin-top: .5rem;
            font-size: .6rem;
            color: #e50038;
        }

        .reminder .reminder-text {
            margin-top: .4rem;
            font-size: .6rem;
            color: #898989;
        }

        .reminder .reminder-text span {
            color: #e50038;
        }
    </style>
    <div class="main">
        <?php
        if ($defaultAddress) {
            ?>
            <a href="<?= Url::to(['mall-m/user-address-list', 'institution_code' => $institution_code, 'is_from_order' => 1]) ?>" class="receiver-address" style="display: block">
                <div class="goods-receipt">
                    <div class="name-num">
                        <p><span class="name"><?= $defaultAddress['name'] ?></span><span class="phone-num"><?= $defaultAddress['phone'] ?></span></p>
                    </div>
                    <div class="address">
                        <p><?= $defaultAddress['province_name'] ?><?= $defaultAddress['city_name'] ?><?= $defaultAddress['region_name'] ?><?= $defaultAddress['address'] ?></p>
                    </div>
                    <div class="goods-receipt-img">
                        <img class="e-resize" src="<?= $web ?>/images/mall-m/e-resize.png"/>
                        <img class="location-icon" src="<?= $web ?>/images/mall-m/location-icon.png" style="display: none;"/>
                        <img class="location-border" src="<?= $web ?>/images/mall-m/location-border.png"/>
                    </div>
                </div>
            </a>
            <?php
        } else {
            ?>
            <a href="<?= Url::to(['mall-m/user-address', 'institution_code' => $institution_code]) ?>" class="receiver-address">
                <div class="goods-receipt">
                    <div class="address-2">
                        <p>您的收货地址为空，点击添加收货地址</p>
                    </div>
                    <div class="goods-receipt-img">
                        <img class="e-resize-2" src="<?= $web ?>/images/mall-m/e-resize.png"/>
                        <img class="location-border" src="<?= $web ?>/images/mall-m/location-border.png"/>
                    </div>
                </div>
            </a>
            <?php
        }
        ?>


        <div class="boundary"></div>
        <div class="order-from">
            <div class="title">
                <p>确认订单信息</p>
            </div>
            <div class="order-from-content">
                <?php
                $sum_integral = 0;
                $sum_quantity = 0;
                foreach ($cartGoodsList as $item) {
                    $sum_integral = $sum_integral + $item['selling_integral_sum'];
                    $sum_quantity = $sum_quantity + $item['quantity'];
                    ?>
                    <div class="order-from-content1">
                        <a href="goods-detail?goods_code=<?= $item['goods_code'] ?>">
                            <img class="f-l" src="<?= $item['goods_image'] ?>"/>
                        </a>
                        <div class="order-from-content1-text f-r">
                            <p class="text crossborder">
                                <?php
                                if ($item['is_crossborder'] == '1') {
                                    ?>
                                    <span>跨境</span>
                                    <?php
                                }
                                ?>
                                <?= $item['name'] ?>
                            </p>
                            <div class="info">
                                <p class="mode f-l">支付方式：<span><?= $integral_unit ?></span></p>
                                <p class="num f-l">数量：<span><?= $item['quantity'] ?></span></p>
                                <p class="price f-r">单价：<span><?= intval($item['selling_integral']) ?><?= $integral_unit ?></span></p>
                            </div>
                        </div>
                    </div>
                    <?php
                }
                ?>
                <!--<div class="order-from-content1">
                <a href="#">
                    <img class="f-l" src="<? /*= $web */ ?>/images/mall-m/order-from-content1-img.png"/>
                </a>
                <div class="order-from-content1-text f-r">
                    <p class="text">女神范显高勾搭术 拍下立减5元 </p>
                    <div class="info">
                        <p class="mode f-l">支付方式：<span>积分</span></p>
                        <p class="num f-l">数量：<span>1</span></p>
                        <p class="price f-r">单价：<span>99积分</span></p>
                    </div>
                </div>
            </div>-->


            </div>

            <div class="leave-word">
                <p>买家留言：<input type="text" id="txt_remark" placeholder="请尽快发货..."></p>
            </div>


            <div class="stat">
                <?php
                $pay_integral = $sum_integral;
                if ($is_cash_order == '1' && $pay_integral > $userIntegral) {
                    $pay_integral = $userIntegral;
                    $pay_cash = round(($sum_integral - $pay_integral) / $integral_rate, 2);
                    ?>
                    <p class="stat-1 f-r">现金：<span><?= $pay_cash ?>￥</span></p>
                    <?php
                }
                ?>

                <p class="stat-1 f-r">合计：<span><?= intval($pay_integral) ?><?= $integral_unit ?></span></p>
                <p class="stat-2 f-r">共<span><?= $sum_quantity ?></span>件商品</p>
            </div>
            <a href="<?= Url::to(['mall-m/goods-list', 'integralTo' => $userIntegral - $pay_integral, 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
                <div class="remain">
                    <p class="remain-left f-l">剩余：<span><?= intval($userIntegral) ?><?= $integral_unit ?></span></p>
                    <img class="f-r" src="<?= $web ?>/images/mall-m/a-icon.png"/>
                    <p class="remain-right f-r">可以兑换商品</p>
                </div>
            </a>
            <?php
            if ($is_crossborder == '1') {
                ?>
                <div class="identity">
                    <p class="remain-left f-l">身份证姓名：</p>
                    <input class="name f-l" type="text" id="txt_idcard_name" value=""/>
                </div>
                <div class="identity">
                    <p class="remain-left f-l">身份证号码：</p>
                    <input class="num f-l" type="text" id="txt_idcard_number" value=""/>
                </div>
                <a href="javascript:">
                    <div class="remain cross-border">
                        <p class="remain-left f-l">跨境保税商品购买须知</p>
                        <img class="f-r" src="<?= $web ?>/images/mall-m/a-icon.png"/>

                    </div>
                </a>
                <div class="cross-text">
                    <p>一、什么是跨境保税商品</p>
                    <p>跨境保税商品是采购自国外的商品，进境时海关、国检都会对商品的品质做审查（品质保证），进境后存放于保税区仓库，国内消费者通过电子商务平台下单购买后，在海关全程监管下打包出区，最终交由国内快递送至消费者手上。优点：商品进境时海关、国检对商品审查，商品存放保税仓库（绝无假货，品质保证）</p>
                    <p>二、购买跨境保税商品要求</p>
                    <p>1、单笔订单不超过<span>2000元</span>人民币；</p>
                    <p>2、每人次一年不能购买超过2万元人民币跨境保税商品（按身份证号计算）；</p>
                    <p>3、根据中国海关总署要求，您所购买的商品需提供<span>身份证信息</span>进行入境申报，请您配合。我们不会向第三方泄露您的资料，请您放心。</p>
                    <p>三、跨境商品物流配送</p>
                    <p>跨境产品由于海关检验的特殊性，物流送达时间一般时效为：</p>
                    <p>保税区邮：<br/>保税区—圆通，时效<span>5-7天</span></p>
                    <p>香港直邮：<br/>香港外包仓—申通快递，时效<span>10天左右</span><br/>香港汇通仓—汇通快递，时效<span>8-10天</span><br/>香港—圆通，时效<span>10-12天</span></p>
                    <p>海外直邮：<br/>日本直邮—EMS，时效<span>5-7天</span><br/>日本—国际段联邦国内转邮政小包或韵达，时效<span>10-13天</span><br/>荷兰—中欧快运，时效<span>10-15天</span></p>
                    <div class="reminder">
                        <p class="reminder-name">温馨提示：</p>
                        <p class="reminder-text">由于海关系统升级于<span>16年12月30日18:00</span>后兑换的跨境产品，需要<span>17年1月10日</span>开始发货，给您带来的不变敬请谅解。</p>
                    </div>
                </div>
                <script type="text/javascript">
                    $(function () {
                        var i = 0;
                        $(".cross-border").click(function () {
                            if (i == 0) {
                                $(".cross-text").show();
                                $(".cross-border img").addClass("cross-img");
                                i = 1;
                            } else if (i == 1) {
                                $(".cross-text").hide();
                                $(".cross-border img").removeClass("cross-img");
                                i = 0;
                            }

                        });
                        $("#txt_idcard_name").focus();
                    });
                </script>
                <?php
            }
            ?>
<!--            <div class="reminder">-->
<!--                <p class="reminder-name">温馨提示：</p>-->
<!--                <p class="reminder-text">由于物流放假,春节期间所兑换的产品均年后发货,放假时间为<span>2017年1月18日 至 2017年2月5日</span>。给您带来的不便敬请谅解。</p>-->
<!--            </div>-->
            <div class="affirm-btn">
                <a type="button" href="javascript:payExec()">确认支付</a>
            </div>
        </div>

    </div>
    <script>
        var is_crossborder =<?=$is_crossborder?>;
        var selling_price_crossborder =<?=$selling_price_crossborder?>;
        var is_cash_order =<?=$is_cash_order?>;
        var cash_integral = 0;
        //
        payExec = function () {
            //跨境商品超过2000价格，不允许下单
//        alert(selling_price_crossborder);
            if (selling_price_crossborder > 2000) {
                alert('跨境商品超过2000价格，不允许下单');
                return;
            }
            if (is_crossborder == 1) {//校验身份证和姓名
                if ($('#txt_idcard_name').val()._trim() == '') {
                    alert('请填写身份证姓名！');
                    return;
                }
                var Validator = new IDValidator();
                if (Validator.isValid($('#txt_idcard_number').val()) == false) {
                    alert('请填写正确的身份证号码！');
                    return;
                }
            }

            if ($('.receiver-address .goods-receipt .address-2').length != 0) {
                alert('请填写收货地址！');
                return;
            }
            if (confirm("是否确认支付订单?")) {
                $('.await-window').show();
                var data = {};
                data.remark = $('#txt_remark').val();
                data.idcard_name = $('#txt_idcard_name').val();
                data.idcard_number = $('#txt_idcard_number').val();
                requestJsonMallM('order-submit-handle', data, true, function (re) {
                    if (re.status == 0) {
                        var orderSubmitResult = re.data.orderSubmitResult;
                        if (orderSubmitResult.status == '0') {
                            // alert('生成订单成功');
                            window.location.href = "<?=Url::to(['mall-m/pay-order-success', 'institution_code' => Mall::getCurrentInstitutionCode()])?>" + '&orderId=' + orderSubmitResult.orderId;
                        } else {
                            if (orderSubmitResult.status == '20' && is_cash_order == '1') {
                                //提示是否开始混合支付
                                cash_integral = orderSubmitResult.cash_integral;
                                $('.await-window').hide();
                                $('.popup-window').show();
                                return;
                            }
                            alert(orderSubmitResult.message);
                        }

                    } else {
                        alert(re.message);
                    }
                    $('.await-window').hide();
                });

            }

        };
        payMix = function () {
            $('.popup-window').hide();
            var data = {};
            data.remark = $('#txt_remark').val();
            data.idcard_name = $('#txt_idcard_name').val();
            data.idcard_number = $('#txt_idcard_number').val();
            //开启混合支付流程
            //生成订单
            var re = requestJsonMallM('create-order-handle', data);
            if (re.status == 0) {
                var createOrderResult = re.data.createOrderResult;
                if (createOrderResult.status == '0') {
                    var orderCode = createOrderResult.orderCode;
                    if (is_weixin()) {
                        invoke_weixinPay(cash_integral, orderCode);
                    } else {
                        $('#alipayment [name="integral"]').val(cash_integral);
                        $('#alipayment [name="order_code"]').val(orderCode);
                        $('#alipayment').submit();
                    }
                } else {
                    alert(createOrderResult.message);
                }
            } else {
                alert(re.message);
            }
        };
        //微信支付入口
        var jsApiParameters = {};
        var out_trade_no = '';
        var v_orderCode = '';
        function invoke_weixinPay(integral, orderCode) {
            var data = {};
            data.integral = integral;
            data.orderCode = orderCode;
            var re = requestJsonMallM("<?=Url::to(['pay-integral/mall-m-weixin-pay-handle'])?>", data);
            if (re.status == 0) {
                jsApiParameters = re.data.weixinpayJs;
                out_trade_no = re.data.out_trade_no;
                v_orderCode = orderCode;
            } else {
                alert(re.message);
                window.location.reload(true);
                return;
            }

            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        }


        //微信支付
        onBridgeReady = function () {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                jsApiParameters,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        window.location.href = "<?=Url::to(['mall-m/pay-order-success', 'institution_code' => Mall::getCurrentInstitutionCode()])?>" + '&isMix=1&out_trade_no=' + out_trade_no;
                    } else {
                        if (res.err_msg == "get_brand_wcpay_request:cancel") {
                            alert("支付取消");
                        } else {
//                        alert(JSON.stringify(res));
                            if (res.err_code == '3') {//扫码支付
                                showWxPayCode(cash_integral, v_orderCode);
                                return;
                            } else {
                                alert("支付错误:" + res.err_desc);
                            }
                        }

                        window.location.href = "<?=Url::to(['mall-m/order-list', 'institution_code' => Mall::getCurrentInstitutionCode()])?>";
                    }
                }
            );
        };


    </script>
    <form id="alipayment" action='<?= Url::to(["pay-integral/mall-alipay-pay"]) ?>' method='post' style="display: none;">
        支付宝支付
        积分:<input name="integral" type="input"/>
        订单号:<input name="order_code" type="input"/>
        <input type="submit" value="支付宝支付" id="btn_ali">
    </form>
    <div class="await-window" style="display: none;">
        <div class="window-content">
            <img src="<?= $web ?>/images/mall-m/await-ing.png"/>
            <p>等待支付完成</p>
        </div>
        <div class="shade"></div>
    </div>
    <div class="popup-window" style="display: none;">
        <div class="popup">
            <div class="popup-heade">
                <p>温馨提醒</p>
                <a href="" class="close-btn">
                    <img src="<?= $web ?>/images/mall-m/pay-window-close.png"/>
                </a>
            </div>
            <div class="popup-content">
                <p class="popup-money">您需要支付现金:<span>￥<?= isset($pay_cash) ? $pay_cash : 0 ?></span></p>
                <p>当您取消订单或退货时，支付的现金部分将以积分形式退还到您的帐户。</p>
            </div>
            <div class="boundary"></div>
            <div class="btn">
                <a href="javascript:$('.popup-window').hide();" class="deny-btn">不，我再想想</a>
                <a href="javascript:payMix();" class="confirm-btn">行，我知道了</a>
            </div>
        </div>
        <div class="shade"></div>
    </div>
    <!--扫码支付-->
<?= \app\widgets\MallMWxNativePay::widget([]) ?>